import axios from 'axios'
import { ElMessage } from 'element-plus'

const service = axios.create({
  baseURL: '/proxy_api'
})

service.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) config.headers['Authorization'] = token
    return config
  }
)

service.interceptors.response.use(
  response => {
    if(response.data.code === 400) {
      ElMessage.error(response.data.msg )
    }
    return Promise.resolve(response.data.data)
  },
  error => {
    if (error?.response) ElMessage.error(error.response.data.msg || '服务器错误')
    return Promise.reject(error)
  }
)

export default service

